package com.example.threadpool;

import java.util.concurrent.*;

public class Main {

    public static void main(String[] args) {

//        LinkedBlockingQueue workQueue = new LinkedBlockingQueue();
        ArrayBlockingQueue workQueue = new ArrayBlockingQueue(2);
//        ExecutorService newCachedThreadPool = new ThreadPoolExecutor(0,3,2, TimeUnit.SECONDS, workQueue);
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        int millis = 5000;
        newCachedThreadPool.execute(()->{
            System.out.println("1.线程池调用"+Thread.currentThread().getName());
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            System.out.println(workQueue.size());
        });
        newCachedThreadPool.execute(()->{
            System.out.println("2.线程池调用"+Thread.currentThread().getName());
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            System.out.println(workQueue.size());

        });
        newCachedThreadPool.execute(()->{
            System.out.println("3.线程池调用"+Thread.currentThread().getName());
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            System.out.println(workQueue.size());

        });
        newCachedThreadPool.execute(()->{
            System.out.println("4.线程池调用"+Thread.currentThread().getName());
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            System.out.println(workQueue.size());

        });
        newCachedThreadPool.execute(()->{
            System.out.println("5.线程池调用"+Thread.currentThread().getName());
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            System.out.println(workQueue.size());

        });

        new Thread(()->{
            while (!newCachedThreadPool.isTerminated()){
                int poolSize = ((ThreadPoolExecutor) newCachedThreadPool).getPoolSize();
                System.out.println("poolSize:"+poolSize);
                int largestPoolSize = ((ThreadPoolExecutor) newCachedThreadPool).getLargestPoolSize();
                System.out.println("largestPoolSize:"+largestPoolSize);
                long taskCount = ((ThreadPoolExecutor) newCachedThreadPool).getTaskCount();
                System.out.println("taskCount:"+taskCount);
                int activeCount = ((ThreadPoolExecutor) newCachedThreadPool).getActiveCount();
                System.out.println("activeCount:"+activeCount);
                System.out.println("------------------------------------------");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        newCachedThreadPool.shutdown();

    }


}
